﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YiSha.Entity.OrganizationManage;
using YiSha.Entity.RedBookSetManage;
using YiSha.Model.Param.RedBookSetManage;
using YiSha.Service.OrganizationManage;
using YiSha.Service.RedBookSetManage;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Web.Code;

namespace YiSha.Business.RedBookSetManage
{
    public class RedBookSetBll
    {
        private RedBookSetService redBookSetService = new RedBookSetService();
        private UserService userService = new UserService();

        #region 获取数据
        public async Task<TData<List<RedBookSetEntity>>> GetList(RedBookSetListParam param)
        {
            TData<List<RedBookSetEntity>> obj = new TData<List<RedBookSetEntity>>();
            obj.Data = await redBookSetService.GetList(param);
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<RedBookSetEntity>>> GetPageList(RedBookSetListParam param, Pagination pagination)
        {
            TData<List<RedBookSetEntity>> obj = new TData<List<RedBookSetEntity>>();


            OperatorInfo operatorInfo = await Operator.Instance.Current();
            if (!string.IsNullOrEmpty(operatorInfo.RoleNames) && operatorInfo.RoleNames.Contains("管理员"))
            {
            }
            else   //用户角色只能看到自己的
            { 
                param.currentUserId = operatorInfo.UserId;
            }
       
            obj.Data = await redBookSetService.GetPageList(param, pagination);
           
        
            obj.Total = pagination.TotalCount;
            obj.Tag = 1;
          
            return obj;
        }
        public async Task<TData<RedBookSetEntity>> GetEntity(long id)
        {
            TData<RedBookSetEntity> obj = new TData<RedBookSetEntity>();
            RedBookSetEntity redBookSetEntity = await redBookSetService.GetEntity(id);
            obj.Data = redBookSetEntity;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<int>> GetMaxSort()
        {
            TData<int> obj = new TData<int>();
            obj.Data = await redBookSetService.GetMaxSort();
            obj.Tag = 1;
            return obj;
        }

        public async Task<List<RedBookSetEntity>> GetJobList()
        {
        
            return await redBookSetService.GetJobList();
            
        }

   
        #endregion

        #region 提交数据
        public async Task<TData<string>> SaveForm(RedBookSetEntity entity)
        {
            TData<string> obj = new TData<string>();
        
            await redBookSetService.SaveForm(entity);
            obj.Data = entity.Id.ParseToString();
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData> DeleteForm(string ids)
        {
            TData obj = new TData();
            await redBookSetService.DeleteForm(ids);
            obj.Tag = 1;
            return obj;
        }
        #endregion

        #region 公共方法
        /// <summary>
        /// 获取当前部门及下面所有的部门
        /// </summary>
        /// <param name="departmentList"></param>
        /// <param name="departmentId"></param>
        /// <returns></returns>
        //public async Task<List<long>> GetChildrenDepartmentIdList(List<RedBookSetEntity> departmentList, long departmentId)
        //{
        //    if (departmentList == null)
        //    {
        //        departmentList = await departmentService.GetList(null);
        //    }
        //    List<long> departmentIdList = new List<long>();
        //    departmentIdList.Add(departmentId);
        //    GetChildrenDepartmentIdList(departmentList, departmentId, departmentIdList);
        //    return departmentIdList;
        //}
        #endregion 

        #region 私有方法
        /// <summary>
        /// 获取该部门下面所有的子部门
        /// </summary>
        /// <param name="departmentList"></param>
        /// <param name="departmentId"></param>
        /// <param name="departmentIdList"></param>
        //private void GetChildrenDepartmentIdList(List<RedBookSetEntity> departmentList, long departmentId, List<long> departmentIdList)
        //{
        //    var children = departmentList.Where(p => p.ParentId == departmentId).Select(p => p.Id.Value).ToList();
        //    if (children.Count > 0)
        //    {
        //        departmentIdList.AddRange(children);
        //        foreach (long id in children)
        //        {
        //            GetChildrenDepartmentIdList(departmentList, id, departmentIdList);
        //        }
        //    }
        //}
        #endregion
    }
}
